using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.IO.Ports;

using SerialPortIO;

namespace SerialPortWriter
{
    public partial class MainForm : Form
    {
        private PortHandler portHandler;

        private string  Port        = null;
        private bool    Pressure    = false;
        
        public MainForm(string port, bool pr)
        {
            InitializeComponent();
            portHandler = new PortHandler(new SerialPort());

            Port        = port;
            Pressure    = pr;
        }

        private void initLookups()
        {
            string[] ports = SerialPort.GetPortNames();
            if (ports.Length == 0)
            {
                MessageBox.Show("No ports found.");
                Application.Exit();
            }
            cbPort.DataSource = ports;
            cbParity.DataSource = Enum.GetValues(typeof(Parity));
            cbStopBits.DataSource = Enum.GetValues(typeof(StopBits));
            cbHandshake.DataSource = Enum.GetValues(typeof(Handshake));

            cbPort.SelectedIndexChanged +=new EventHandler(cbPort_SelectedIndexChanged);
        }

        void cbPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void setDefaults()
        {
            cbHandshake.SelectedItem = Handshake.XOnXOff;
            if(Port != "")
            {
                cbPort.SelectedItem      = Port;
                this.checkBox1.Checked   = Pressure;
                
                portHandler.PortName = Port;
                portHandler.Handshake = Handshake.XOnXOff;
            }

        }

        private void setBindings()
        {
            cbPort.DataBindings.Add(new Binding("SelectedItem", portHandler, "PortName"));
            numUDBaudRate.DataBindings.Add(new Binding("Value", portHandler, "BaudRate"));
            cbParity.DataBindings.Add(new Binding("SelectedItem", portHandler, "Parity"));
            numUDDataBits.DataBindings.Add(new Binding("Value", portHandler, "DataBits"));
            cbStopBits.DataBindings.Add(new Binding("SelectedItem", portHandler, "StopBits"));
            cbHandshake.DataBindings.Add(new Binding("SelectedItem", portHandler, "Handshake"));
        }

        private void writeLog(string filename)
        {
            using (StreamWriter sw = File.AppendText(filename))
            {
                sw.Write(portHandler.ReadBuffer);
                sw.Flush();
                sw.Close();
            }
        }


        #region EVENTS

        private void OnLoad(object sender, EventArgs e)
        {
            initLookups();
            setBindings();
            setDefaults();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            portHandler.StartWriting(checkBox1.Checked);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            portHandler.Stop();
        }

        #endregion




        

        

        

    }
}